<?php

namespace App\Http\Services\FormBuilder\Segments;

use FormBuilder;
use Illuminate\Support\Collection;
use App\Http\Services\FormBuilder\Elements\Text;
use App\Http\Services\FormBuilder\Elements\Hidden;
use App\Http\Services\FormBuilder\Elements\FormControl;
use App\Http\Services\FormBuilder\Traits\MoneySelectorTrait;
use App\Http\Services\FormBuilder\Components\DefaultDescription;
use Context;

class BracketDonate extends FormControl
{
    use MoneySelectorTrait;

    private $brackets;

    public function __construct($name, Collection $data)
    {
        parent::__construct($name);
        $this->name     = $name;
        $this->data     = $data;
        $this->brackets = collect($this->data->get('options'))->map(function ($item) {
            if (!isset($item['id'])) {
                $item['id'] = '';
            }
            $item['desc'] = htmlspecialchars($item['desc']);
            return $item;
        })->all();
    }

    public function render()
    {
        $html = '';

        if ($this->label) {
            $html .= $this->label;
        }

        if ($this->stockLimit()) {
            $html .= $this->renderLimitBracket();
        } else {
            $html .= $this->renderNotLimitBracket();
        }

        if ($this->allowCustomMoney()) {
            $html .= $this->renderCustomMoneyInput();
        }

        if ($this->allowOverGoal()) {
            $html .= $this->renderAllowOverGoal();
        }

        $html .= (new Hidden($this->name))->id('hidden_goal')
            ->attribute('autocomplete', 'off')
            ->value($this->getDefaultMoney());

        $html .= (new Hidden('money_desc'))->id('money_desc')
            ->attribute('autocomplete', 'off')
            ->value($this->getDefaultDescription());

        $html .= (new Hidden('bracket_id'))->id('bracket_id')
            ->attribute('autocomplete', 'off')
            ->value($this->getDefaultBracketId());

        $html .= <<<HTML
            <div class="after_money"></div>
HTML;


        return $html;
    }

    public function withLabel($label)
    {
        $this->label($label);

        return $this;
    }

    public function brackets($brackets)
    {
        $this->brackets = $brackets;

        return $this;
    }

    private function renderNotLimitBracket()
    {
        $html = '';
        if ($this->showOptionsDesc()) {
            $html = new DefaultDescription($this->getDefaultDescription());
        }
        $bracket = new MoneySelectorBracketNotStockLimit($this->brackets);
        $bracket->interval($this->interval)
            ->setIntervalDisplayName($this->intervalDisplayName)
            ->defaultMoney($this->getDefaultMoney())
            ->unit($this->unit);

        return $html . $bracket;
    }

    private function renderLimitBracket()
    {
        $bracket = new MoneySelectorBracketWithStockLimit($this->brackets);
        $bracket->interval($this->interval)
            ->setIntervalDisplayName($this->intervalDisplayName)
            ->defaultMoney($this->getDefaultMoney())
            ->unit($this->unit);

        return $bracket;
    }

    private function renderCustomMoneyInput()
    {
        $moneySet = $this->getMoneySet();
        $text     = (new Text(''))->id('other_goal')->attribute('autocomplete', 'off');
        if (!$moneySet->contains($this->getDefaultMoney())) {
            $text->defaultValue($this->getDefaultMoney());
        }

        $moneyInput = new MoneyInput($text);

        if ($this->allowEmptyAndNotLimit()) {
            $moneyInput->placeholder(lang('goal_or_empty_is_unlimited')->write());
        }

        $moneyInput->prependIcon($this->getMoneyInputPrependIcon(), 'icon-left')
            ->appendIcon($this->getMoneyInputAppendIcon(), 'icon-right')
            ->removeClass('left')
            ->addClass('right');

        return '<div class="field">' . $moneyInput . '</div>';
    }

    private function getMoneyInputPrependIcon()
    {
        if ($this->allowEmptyAndNotLimit()) {
            return '';
        }

        if (!$this->getMoneySet()->isEmpty()) {
            return is_english() ? 'cf-other-money-english' : 'cf-other-money';
        } else {
            return is_english() ? 'cf-pay-money-english' : 'cf-pay-money';
        }
    }

    private function getMoneyInputAppendIcon()
    {
        if ($this->isIntervalDonate()) {
            return get_RMB_icon_name($this->interval);
        } else {
            return is_english() ? 'cf-yuan-english' : 'cf-yuan';
        }
    }

    // 达到目标之后，是否可以继续筹款
    private function renderAllowOverGoal()
    {
        $html = FormBuilder::buildCheckbox([
            'value' => Context::get('user_project') ? Context::get('user_project.allow_over_goal') : 1,
            'class' => 't5margin',
            'name' => 'allow_over_goal',
            'display_name' => lang('allow_over_goal')->write(),
        ]);

        return $html;
    }
}
